#include <blib.h>

size_t strlen(const char *s) {
	size_t length = 0;
	while (*s != '\0') {
		length++;
		s++;
	}
	return length;
}

char *strcpy(char *dst, const char *src) {
	char *dst_ori = dst;
	while (*src != '\0') {
		*dst = *src;
		dst++;
 		src++;
	}
	*dst = '\0';
	return dst_ori;
}

char *strncpy(char *dst, const char *src, size_t n) {
	char *res = dst;
	while (*src && n--) {
		*dst++ = *src++;
	}
	*dst = '\0';
	return res;
}

char *strcat(char *dst, const char *src) {
	char* dst_ori = dst;
	while(*dst!='\0'){
		dst++;
	}
	while(*src!='\0'){
		*dst = *src;
		dst++;
		src++;
	}
	return dst_ori;
}

int strcmp(const char *s1, const char *s2) {
	int i = 0;
	while (s1[i] != '\0' && s2[i] != '\0') {
        	if (s1[i] < s2[i]) {
			return -1;
		} else if (s1[i] > s2[i]) {
			return 1;
		}
		i++;
	}
	if (s1[i] == '\0' && s2[i] == '\0') {
        	return 0;
	} else if (s1[i] == '\0') {
		return -1;
	} else {
		return 1;
	}
}

int strncmp(const char *s1, const char *s2, size_t n) {
	while (n--) {
		if (*s1 != *s2) {
			return *s1 - *s2;
		}
		if (*s1 == 0) {
			break;
		}
		s1++;
		s2++;
	}
	return 0;
}

void *memset(void *s, int c, size_t n) {
	char *p = s;
	while (n-- > 0) {
		*p++ = c;
	}
	return s;
}

void *memcpy(void *out, const void *in, size_t n) {
	char *csrc = (char *)in;
	char *cdest = (char *)out;
	for (int i = 0; i < n; i++) {
		cdest[i] = csrc[i];
	}
	return out;
}

int memcmp(const void *s1, const void *s2, size_t n) {
	const unsigned char *p1 = s1, *p2 = s2;
	while (n--) {
		if (*p1 != *p2) {
			return *p1 - *p2;
		}
		p1++;
		p2++;
	}
	return 0;
}
